package org.lgl.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.lgl.goods.dao.BrandDao;
import org.lgl.goods.model.Brand;
import org.lgl.goods.service.BrandService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author lgl
 * @date 2022/2/25 14:26
 */
@Service
public class BrandServiceImpl implements BrandService {

    private final BrandDao brandDao;

    public BrandServiceImpl(BrandDao brandDao) {
        this.brandDao = brandDao;
    }

    @Override
    public Brand getById(Long id) {
        return brandDao.selectByPrimaryKey(id);
    }

    @Override
    public long addBrand(Brand brand) {
        return brandDao.insert(brand);
    }

    @Override
    public List<Brand> searchList(Brand brand) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (null != brand) {
            if (null != brand.getId()) {
                criteria.andEqualTo("id", brand.getId());
            }
            if (StringUtils.hasLength(brand.getName())) {
                criteria.andLike("name", "%" + brand.getName() + "%");
            }
            if (StringUtils.hasLength(brand.getBeginLetter())) {
                criteria.andEqualTo("beginLetter", brand.getBeginLetter());
            }
        }
        return brandDao.selectByExample(example);
    }

    @Override
    public PageInfo<Brand> listWithPage(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(brandDao.selectAll());
    }
}
